package net.area80.utils
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.AsyncErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import org.osflash.signals.Signal;
	
	public class SmartLoader extends EventDispatcher
	{
		private var PATH:String;
		private var currentFileType:String;
		
		public static const TYPE_JPG:String = "jpg";
		public static const TYPE_SWF:String = "swf";
		public static const TYPE_XML:String = "xml";
		public static const TYPE_TEXT:String = "txt";
		
		public var reloadIfFailed:Boolean = true;
		public var SIGNAL_COMPLETE:Signal = new Signal(Object);
		private var postParams:URLVariables;
		
		public function SmartLoader(reloadIfFailed:Boolean=true):void
		{	
			this.reloadIfFailed = reloadIfFailed;
		}
		public function load (path:String, forceType:String="", postParams:URLVariables = null):void {
			PATH = path;
			this.postParams = postParams;
			currentFileType = (forceType!="") ? forceType : getType();
			doLoad();
		}
		private function getType ():String {
			var type:String = PATH.split(".")[PATH.split(".").length-1].split("?")[0].split("#")[0];
			return type;
		}
		private function doLoad ():void {
			switch (currentFileType) {
				case "xml":
				case "php":
				case "asp":
				case "aspx":
				case "txt":
				case "dae":
				var uldr:URLLoader = new URLLoader();
				var p:URLRequest = new URLRequest(PATH);
				if(postParams) {
					p.method = "POST";
					p.data = postParams;
				}
				uldr.addEventListener(Event.COMPLETE, completeHandler);
				uldr.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				uldr.addEventListener(ProgressEvent.PROGRESS, progressHandler);
				uldr.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
				uldr.load(p);
				break;
				case "swf":
				case "jpg":
				case "png":
				case "jpeg":
				case "gif":
				var ldr:Loader = new Loader();
				ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
				ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
				ldr.contentLoaderInfo.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
				ldr.load(new URLRequest(PATH));
				break;
			}
		}
		private function completeHandler (e:Event):void {
			dispatchEvent(e);
			switch (currentFileType) {
				case "xml":
				case "php":
				case "asp":
				case "aspx":
				case "txt":
				case "dae":
				SIGNAL_COMPLETE.dispatch(URLLoader(e.currentTarget).data);
				break;
				case "swf":
				case "jpg":
				case "png":
				case "jpeg":
				case "gif":
				SIGNAL_COMPLETE.dispatch(LoaderInfo(e.currentTarget).content.parent);
				break;
			}
		}
		private function ioErrorHandler (e:Event):void {
			trace("SmartLoader:IOERROR"+PATH)
			dispatchEvent(e);
			if(reloadIfFailed) doLoad();
		}
		private function progressHandler (e:Event):void {
			dispatchEvent(e);
		}
		private function asyncErrorHandler (e:Event):void {
			dispatchEvent(e);
			if(reloadIfFailed) doLoad();
		}

	}
}